﻿//"use strict";
(function ($) {
    $.fn.extend({
        randomize: function (selector) {
            (selector ? this.children(selector) : this).parent().each(function () {
                $(this).children(selector).sort(function () {
                    return Math.random() - 0.5;
                }).detach().appendTo(this);
            })
            return this;
        },
        rotaterator: function (options) {
            var defaults = {
                fadeSpeed: 600,
                pauseSpeed: 100
            }
            var o = $.extend({}, defaults, options)
            return this.each(function () {
                var obj = $(this),
					maxHeight = 0,
					items = obj.children().each(function () {
					    maxHeight = Math.max(maxHeight, $(this).height())
					}).slice(1).hide().end(),
					next = items.eq(0),
					hovering = false,
					delay = o.pauseSpeed + o.fadeSpeed,
					timeout;
                obj.height(maxHeight).hover(function () {
                    hovering = true
                }, function () {
                    hovering = false
                    clearTimeout(timeout)
                    timeout = setTimeout(recurse, 1000)
                })
                timeout = setTimeout(recurse, o.pauseSpeed)
                function recurse() {
                    //	console.log('recurse',obj.children(':visible'),hovering,delay)
                    if (hovering)
                        timeout = setTimeout(recurse, delay)
                    else
                        obj.children(':visible').stop().fadeOut(o.fadeSpeed, function () {
                            next = $(this).next();
                            if (next.length == 0) {
                                next = items.eq(0)
                            }
                            next.fadeIn(o.fadeSpeed)
                            timeout = setTimeout(recurse, delay)
                        });
                }
            });
        }
    });
})(jQuery);

